﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using Microsoft.Practices.Unity;
using NHibernate;

namespace Epona.NHibernateRepository
{
    public class SessionProvider:ISessionProvider
    {
        private readonly ThreadLocal<ISession> sessionHolder = new ThreadLocal<ISession>();

        //TODO 构造函数注入
        [Dependency]
        public ISessionFactory SessionFactory { get;  set; }

        public ISession Session
        {
            get
            {
                ISession result = sessionHolder.Value;
                if (result != null && result.IsOpen)
                {
                    return result;
                }
                result = SessionFactory.GetCurrentSession();
                sessionHolder.Value = result;
                return result;
            }
        }
    }
}
